//
//  PersonTool.swift
//  JGWB
//
//  Created by 付建国 on 2016/10/11.
//  Copyright © 2016年 sher. All rights reserved.
//

import UIKit

class PersonViewModel {
    
    // MARK:- 单例
    static let shareIntance : PersonViewModel = PersonViewModel()
    
    // MARK:- 定义属性
    var account : Person?
    
    // MARK:- 计算属性
    var accountPath : String {
        let accountPath = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true).first!
        return (accountPath as NSString).strings(byAppendingPaths: ["account.plist"]).first!

    }
    var isLogin : Bool{
        if account == nil {
            return false
        }
        
        guard let expiresDate = account?.expires_date else{
            return false
        }
        
        return expiresDate.compare(Date()) == ComparisonResult.orderedDescending
    }
    
    // MARK:- 重写init
    init() {
        //1.从沙盒中读取归档信息
        account = NSKeyedUnarchiver.unarchiveObject(withFile: accountPath) as? Person
        
    }
}
